var roleManage = function() {

	var _init = function() {
		// 表单验证
		// 关闭弹框
		$(".modal").on('hidden.bs.modal', function(e) {
			$('.modal .help-block').html(''); // 清空提示
//			$('.modal input').val(''); // 清空 input
		});
		// 添加
		$('#btn_new_role').click(function(event) {
			$('#modal_new_role').modal('show');
		});
		// 确定添加
		$('#save').click(function() {
			  var _data = $("#modal_new_role").serializeObject();
/*				var templateId =$("#modal_new_role input[name='templateId']:checked").val(); 
*/
	       if (!mdTool.validate(_data.phone, 'require')) {
				mdTool.showMessage('请输入手机号码');
				return;
			}if (!mdTool.validate(_data.phone, 'phone')) {
				mdTool.showMessage('手机号码格式不正确，请重新输入');
				return;
			}if (!mdTool.validate(_data.tname, 'require')) {
				mdTool.showMessage('请输入联系人姓名');
				return;
			}if (!mdTool.validate(_data.templateId, 'require')) {
				mdTool.showMessage('请选择模板');
				return;
			}if (!mdTool.validate(_data.category, 'require')) {
				mdTool.showMessage('请选择类目');
				return;
			}if (!mdTool.validate(_data.name, 'require')) {
				mdTool.showMessage('请输入店铺名称');
				return;
			}if (!mdTool.validate(_data.address, 'require')) {
				mdTool.showMessage('请输入地址');
				return;
			}
			//先判斷可以添加商戶不（套數夠不）
			$("#save").attr("disabled", true); 
		$.ajax({
//			$('#save').attr("disabled","disabled");
			type : "get",
			url : "/a/biz/isAdd",
			success : function(data) {
				if(data.status == "1"){
					var param={"tenant":{"id":_data.id,"phone":_data.phone,"loginName":_data.loginName,"password":_data.password,"name":_data.name,"templateId":_data.templateId,"category":_data.category,
		                 "province":_data.province,"city":_data.city,"address":_data.address},"operator":{"phone":_data.phone}}
					$.ajax({
						url : '/a/biz/tenant/tenant',
						type : 'PUT',
						dataType : 'json',
						contentType:"application/json; charset=utf-8",
						data : JSON.stringify(param),
					}).done(function(data) {
						if (data.status === '1') {
							// 正确
				        	window.location.href="/p/biz/tenant";
						} else if (data.status === '2') {
//							错误
							mdTool.showMessage(data.message);
							$('#save').attr("disabled",false);
//							$('#modal_new_role .help-block').html(data.message); // 返回错误信息
						}
					});
				}else{
					alert("代理商套数不足！");
					$('#save').attr("disabled",false);
					return false;
				}
			},
			error : function() {
				return false;
			}
		});
			
		});



	}

	return {
		init : function() {
			_init();
		}

	};

}();

$(function() {
	roleManage.init();
	
});


function add(){
	window.location.href="/p/biz/tenant/totenant";
	}
function getPwdandAd(){
	var phone = 	$("input[name='phone']").val();
	$("input[name='loginName']").val(phone);
	$("input[name='password']").val(phone);
	$("input[name='loginName1']").val(phone);
	$("input[name='password1']").val(phone);
}


$(document).ready(function(){
	
		$("button[name='show']").on("click",function(){
			 var cpid = $(this).parent().parent().find("td").eq(1).text();
			 window.location.href="/p/biz/tenant/show?id="+ encodeURIComponent(cpid);
		  });
		
		$("button[name='delete']").click(function(){
			 var cpid = $(this).parent().parent().find("td").eq(1).text();
			window.location.href="/p/biz/tenant/delete?id="+ encodeURIComponent(cpid);
		  });
});

function getCheck(){
	
	 var url= "/a/biz/tenant/tocheck";      
         $.ajax({
             type: "POST", 
             dataType: "json",
            // async:false, 
             url:url,
             data: $('#form1').serialize(),
             success: function (result) {
            	 $("#tb_content").html("");
            	 var str = "";
            	 $.each(result.data,function(i){
            	 str = "<tr>" +  
                     "<td ><input type='checkbox' class='checkboxes' value='1' /></td>" +  
                     "<td class='hidden-480'><a>"+result.data[i].id+"</a></td>" +  
                     "<td class='hidden-480'><a>"+result.data[i].name+"</a></td>" +  
                     " <td class='hidden-480'><a>"+result.data[i].phone+"</a></td>" +  
                     "<td class='hidden-480'><a>"+result.data[i].province+"</a></td>" +  
                     "<td class='hidden-480'><a>"+result.data[i].city+"</a></td>" +  
                     "<td class='hidden-480'><a>"+result.data[i].templateId+"</a></td>" +  
                     "<td class='hidden-480'><a></a></td>" +  
                     "<td class='hidden-480'><a>"+result.data[i].createDate+"</a></td>" +  
                    " <td ><button class=\'btn green\' name=\'show\' type=\'button\' >查看</button>"+
					" <button class=\'btn green\' name=\'delete\' type=\'button\' >删除</button></td>"+
                     "</tr>" ;
            	 });
            	 $("#tb_content").append(str);
            	 
            	 $("button[name='show']").on("click",function(){
         			 var cpid = $(this).parent().parent().find("td").eq(1).text();
         			 window.location.href="/p/biz/tenant/show?id="+ encodeURIComponent(cpid);
         		  });
         		
         		$("button[name='delete']").click(function(){
         			 var cpid = $(this).parent().parent().find("td").eq(1).text();
         			window.location.href="/p/biz/tenant/delete?id="+ encodeURIComponent(cpid);
         		  });
             },
             error: function(data) {
               
              }

         });
	
}

function beback(){
	window.location.href= "/p/biz/tenant";
}

/*$(".labelauty input[type='radio']").on("change",function(){
	var templateId=$(".labelauty input[type='radio'] :checked").val();
	$.ajax({
		url : '/a/biz/getCategory',
		type : 'POST',
		dataType : 'json',
		contentType:"application/json; charset=utf-8",
		 async: false,
		data : JSON.stringify({ template: templateId }),
	}).done(function(data) {
		if (data.status === '1') {
			 $('select[name="category"]').html("<option value=''  disabled selected>请选择类目</option>");
			for (var i = 0; i < data.data.length; i++) {
		        $('select[name="category"]').append("<option>" + data.data[i].category + "</option>");
		      }
			$('select[name="category"]').html(" <option th:each='industry : ${listIndustry}' th:text='${industry.category}' th:value='${industry.category}'></option>")
		} else if (data.status === '2') {
		}
	});
})*/



$(".labelauty label").click(function() {
    if ($(this).is(".active")) {
      $(this).removeClass("active");
      $(this).find("input[type='radio']").prop("checked", false);
    /*  return false;*/
    } else {
      $(this).addClass("active");
      $(this).siblings("label").removeClass("active");
      $(this).find("input[type='radio']").prop("checked", true);
     /* return false;*/
    }
    
	var templateId=$(".labelauty input[type='radio']:checked").val();
	$.ajax({
		url : '/a/biz/getCategory',
		type : 'POST',
		dataType : 'json',
		contentType:"application/json; charset=utf-8",
		 async: false,
		data : JSON.stringify({ template: templateId }),
	}).done(function(data) {
		if (data.status === '1') {
			 $('.category').html("");
				for (var i = 0; i < data.data.length; i++) {
			        $('.category').append("<label  class=\"btn btn-default \" for="+ data.data[i].category +" ><input id="+ data.data[i].category +" name=\"category\" type=\"radio\" value="+ data.data[i].category +" >"+ data.data[i].category+"</label></option>");
			      
			      }
				  $(".category label").click(function() {
			            if ($(this).is(".active")) {
			              $(this).removeClass("active");
			              $(this).find("input[type='radio']").prop("checked", false);
			              ;
			            } else {
			              $(this).addClass("active");
			              $(this).siblings("label").removeClass("active");
			              $(this).find("input[type='radio']").prop("checked", true);
			              return false;
			            }
			            return false
			          });
		} else if (data.status === '2') {
		}
	});
	return false;
  });
/*var indexI=0;
function checkRadio(obj){
	   indexI=
	   if ($(obj).is(".active")) {
		      $(obj).removeClass("active");
		      $(obj).find("input[type='radio']").prop("checked", false);
		      ;
		    } else {
		      $(obj).addClass("active");
		      $(obj).siblings("label").removeClass("active");
		      $(obj).find("input[type='radio']").prop("checked", true);
		      return false;
		    }
//		    return false
}*/


$(".category label").click(function() {
    if ($(this).is(".active")) {
      $(this).removeClass("active");
      $(this).find("input[type='radio']").prop("checked", false);
      ;
    } else {
      $(this).addClass("active");
      $(this).siblings("label").removeClass("active");
      $(this).find("input[type='radio']").prop("checked", true);
      return false;
    }
    return false
  });
	

